package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyRecRecordBean;
import flc.ast.databinding.ActivityBusinessLicenseRecBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.apis.base.OcrBusinessLicenseRet;
import stark.common.basic.event.EventStatProxy;

public class BusinessLicenseRecActivity extends BaseAc<ActivityBusinessLicenseRecBinding> {

    public static OcrBusinessLicenseRet sOcrBusinessLicenseRet;
    private String imgPath;

    @Override
    protected int onCreate() {
        return R.layout.activity_business_license_rec;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivBusinessLicenseRecBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivBusinessLicenseImg.setOnClickListener(this);
        mDataBinding.ivCopyResult.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivBusinessLicenseImg://营业执照
                RecCameraActivity.type = 4;
                Intent intent = new Intent(this, RecCameraActivity.class);
                startActivityForResult(intent, Content.BUSINESS_LICENSE_SEL);
                break;
            case R.id.ivCopyResult://复制结果
                String content = getString(R.string.company_name_text) + sOcrBusinessLicenseRet.words_result.companyName.words + "\n" + getString(R.string.type_text) + sOcrBusinessLicenseRet.words_result.type.words + "\n" +
                        getString(R.string.legal_person_text) + sOcrBusinessLicenseRet.words_result.legalPerson.words + "\n" + getString(R.string.capital_text) + sOcrBusinessLicenseRet.words_result.registeredCapital.words + "\n" +
                        getString(R.string.establishment_date_text) + sOcrBusinessLicenseRet.words_result.registeredCapital.words + "\n" + getString(R.string.company_indate_text) + sOcrBusinessLicenseRet.words_result.validStartDate.words + "\n" +
                        getString(R.string.company_address_text) + sOcrBusinessLicenseRet.words_result.address.words + "\n" + getString(R.string.business_scope_text) + sOcrBusinessLicenseRet.words_result.businessScope.words + "\n" +
                        getString(R.string.code_text) + sOcrBusinessLicenseRet.words_result.socialCode.words + "\n" + getString(R.string.number_text) + sOcrBusinessLicenseRet.words_result.idNumber.words + "\n";
                ClipboardUtils.copyText(content);
                ToastUtils.showShort(R.string.copy_success_tips);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void setData() {
        mDataBinding.tvName.setText(sOcrBusinessLicenseRet.words_result.companyName.words);
        mDataBinding.tvType.setText(sOcrBusinessLicenseRet.words_result.type.words);
        mDataBinding.tvLegalPerson.setText(sOcrBusinessLicenseRet.words_result.legalPerson.words);
        mDataBinding.tvCapital.setText(sOcrBusinessLicenseRet.words_result.registeredCapital.words);
        mDataBinding.tvEstablishmentDate.setText(sOcrBusinessLicenseRet.words_result.registeredCapital.words);
        mDataBinding.tvIndate.setText(sOcrBusinessLicenseRet.words_result.validStartDate.words);
        mDataBinding.tvAddress.setText(sOcrBusinessLicenseRet.words_result.address.words);
        mDataBinding.tvBusinessScope.setText(sOcrBusinessLicenseRet.words_result.businessScope.words);
        mDataBinding.tvCode.setText(sOcrBusinessLicenseRet.words_result.socialCode.words);
        mDataBinding.tvNumber.setText(sOcrBusinessLicenseRet.words_result.idNumber.words);
        Glide.with(mContext).load(imgPath).into(mDataBinding.ivBusinessLicenseImg);
        mDataBinding.ivBusinessLicenseUpload.setVisibility(View.VISIBLE);
    }

    private void saveRecord() {
        List<MyRecRecordBean> listSave = new ArrayList<>();
        String content = getString(R.string.legal_person_text) + sOcrBusinessLicenseRet.words_result.legalPerson.words + "\n" + getString(R.string.capital_text) + "\n" + sOcrBusinessLicenseRet.words_result.registeredCapital.words + "\n" +
                getString(R.string.establishment_date_text) + sOcrBusinessLicenseRet.words_result.registeredCapital.words + "\n" + getString(R.string.company_indate_text) + sOcrBusinessLicenseRet.words_result.validStartDate.words + "\n" +
                getString(R.string.company_address_text) + sOcrBusinessLicenseRet.words_result.address.words + "\n" + getString(R.string.business_scope_text) + sOcrBusinessLicenseRet.words_result.businessScope.words + "\n" +
                getString(R.string.code_text) + sOcrBusinessLicenseRet.words_result.socialCode.words + "\n" + getString(R.string.number_text) + sOcrBusinessLicenseRet.words_result.idNumber.words + "\n";
        listSave.add(new MyRecRecordBean(imgPath, sOcrBusinessLicenseRet.words_result.companyName.words, sOcrBusinessLicenseRet.words_result.type.words, content, TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"))));
        List<MyRecRecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveRecordList(listSp);
        } else {
            AppSpUtils.saveRecordList(listSave);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Content.BUSINESS_LICENSE_SEL) {
                imgPath = data.getStringExtra(Content.RecognizeImg);
                setData();
                saveRecord();
            }
        }
    }

}